<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%
	String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="renderer" content="webkit">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>再融资合同添加页面</title>
	<jsp:include page="../../import.jsp" flush="true" />
	<style type="text/css">
		.field a { color:#013d79 ;text-decoration:none;}
		.field a:hover { color:#013d79 ;text-decoration:underline;}
	</style>
</head>
<body class="iframeBody">
	<div class="Wrapper">
		<div>
			<div id="pageInTab" class="pageInTab">
				<div tabid="tabA" title="基本信息" >
					<iframe frameborder="0" name="tabA" id="tabA" src=""></iframe>
				</div>
				<div tabid="tabB" title="资产包信息" id="tabBId" style="display: none;">
					<iframe frameborder="0" name="tabB" id="tabB" src=""></iframe>
				</div>
				<div tabid="tabC" title="首付款信息" id="tabCId" style="display: none;">
					<iframe frameborder="0" name="tabC" id="tabC" src=""></iframe>
				</div>
				<div tabid="tabD" title="还款信息" id="tabDId" style="display: none;">
					<iframe frameborder="0" name="tabD" id="tabD" src=""></iframe>
				</div>
				<!-- 初始化时要显示的页签：合同管理 -->
				<div tabid="tabE" title="合同管理" id="tabEId"  lselected="true">
					<form id="fnc_prj_cont_form" >
						<div class="fieldset1 tabel_col clearFix">
							<div class="caption">
								<span class="title">项目基础信息</span>
							</div>
							<div class="fieldBox col_50">
								<div class="label">项目名称</div>
								<div class="field">
									<span id="fncprjnm" name="fncprjnm">&nbsp;${prjScmCr.FNC_PRJ_NM}</span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">金融机构</div>
								<div class="field">
									<span id="fncinsNM" name="fncinsNM">&nbsp;${prjScmCr.FUND_RAS_INS_NM}</span>
								</div> 
							</div>
							<div class="fieldBox col_50">
								<div class="label">授信名称</div>
								<div class="field">
									<span id="instypeNM" name="instypeNM">&nbsp; <a href="javascript:showDetailCr(${prjScmCr.CR_ID_KEY});" >${prjScmCr.CR_NM}</a></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">融资报价</div>
								<div class="field">
									<span id="fncscmNM" name="fncscmNM">&nbsp;<a href="javascript:showDetailPrice(${prjScmCr.FNC_SCM_ID});" >${prjScmCr.FNC_SCM_NM}</a></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">业务类型</div>
								<div class="field">
									<span id="bustypeNM" name="bustypeNM">&nbsp;${prjScmCr.BUS_TYPE_}</span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">实际融资金额</div>
								<div class="field">
									<span id="acFiAmt" name="acFiAmt"></span>
								</div>
							</div> 
							<div class="fieldBox col_50">
								<div class="label">首付款金额</div>
								<div class="field">
									<span id="firstPayment" name="firstPayment"></span>
								</div>
							</div>
							<div class="fieldBox col_50">
								<div class="label">实际到账金额</div>
								<div class="field">
									<span id="transAm" name="transAm"></span>
								</div>
							</div> 
							<div class="fieldBox col_50">
								<div class="label">到账日期</div>
								<div class="field">
									<span >&nbsp;${prjScmCr.TRANS_TM_}</span>
								</div>
							</div>
							
							<!-- 针对多次来款 -->
							<div class="caption">
								<span class="title">到账明细</span>
							</div>
							<div class="fieldBox col_100">
								<div id="payInfo1" class="sq-grid"></div>
							</div>
							<!-- 合同信息 -->
							<div class="caption">
								<span class="title">合同信息 </span>
								<div style="margin-left: auto;margin-right: 10px;width:200px;margin-top: -15px">
									<span id="cont2_sv_btn_id"
										class="sq-button sq-button-round" onclick="addContract();">新增
									</span>
								</div>
							</div>
							<div class="fieldBox col_100">
								<div id="cont1" class="sq-grid"></div>
							</div>
						</div>
						<!-- 合同主表ID参数 -->
						<div class="fieldBox" style="display:none;">
							<div class="field">
								<input name="mainId" id="mainId" type="text" class="ui-textbox" value="${fncPrjContMain.id}">
							</div>
						</div>
						<!-- 添加和审核修改页面共用 -->
						<c:if test="${from != 'refManager' }">
						<center>
							<div class="btnsBox">
								<span id="addContAuditFlowBtn" class="sq-button sq-button-round" onclick="addContAuditFlow()">发起合同审核流程</span>
								<span id="closeBtn" class="sq-button sq-button-round Hide" onclick="Remove_navTab()">关闭</span>
							</div>
						</center>
						</c:if>
					</form>
				</div>
			</div>
		</div>
	</div>
    
	<script type="text/javascript">
		var pageInApp={
				flag:true
			};
		$(function () {
			initTab();
		    $("#acFiAmt").html(fmoney(new  Number("${prjScmCr.AC_FI_AMT}")/100, 2));
		    $("#firstPayment").html(fmoney(new  Number("${prjScmCr.firstPayment}")/100, 2));
		    $("#transAm").html(fmoney(new  Number("${prjScmCr.TRANS_AM}")/100, 2));
			
			//初始化到账明细
			var id="${prjScmCr.ID}";
			if(id != "") {
				$("#payInfo1").ligerGrid({
		    		url:"<%=path%>/rfbuss/fncprjdrawmon/initList.do?id="+id,
		            method: 'get',
		                columns: [
			                { display: '到账日期', name: 'dmTm', width: 200,format: 'yyyy-MM-dd', editor: { type: 'date'},render:renderDate }, 
			                { display: '到账金额', name: 'dmAm',
			                    editor: { type: 'text' }, align: 'right', width: 150,totalSummary:{
			                        render: function (suminf, column, cell){
			                        	$("#transanttem").val(suminf.sum); 
			                            return '<div>总计:' + suminf.sum.toFixed(2) + '</div>';
			                        }
			                    }
			                },
			                { display: '收款银行', name: 'dueBank',
			                    editor: { type: 'text',readonly:true  }, align: 'center', minWidth: 280
			                },
			                { display: '收款账号', name: 'dmAcc', 
			                    editor: { type: 'text',readonly:true }, align: 'center', width: 180
			                },
			                { display: '账号编号', name: 'accId',hide: true, 
			                    editor: { type: 'text' }, align: 'center', width: 180
			                }			               
		                ],
		                onSelectRow: function (rowdata, rowindex)
		                {
		                },
		                usePager: false,
		                root: 'rows',
		                record: 'total',
		                hideLoadButton: true,
		                width: '100%',
		                height: '100%',
		                heightDiff: -2,
		                enabledEdit: false,
		                isScroll: false,
		                checkbox: false,
		                rownumbers: true,
		                allowUnSelectRow: true,
		                alternatingRow: false,
		                enabledSort: false
				});
			}
		});
		
		//格式化时间
		function renderDate(rowdata, index, value){
			var dateStr = "";
			if(null != value){
				var date = new Date(value);
				dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
			}
			return dateStr;
		}
		
		//初始化Tab
		var pageInTab = null;
	  	var loadedTabs=[];
		function initTab(){
			pageInTab = $("#pageInTab").ligerTab({
				height: 'auto',
				contextmenu: false,
				minIframeHeight: GET_minIframeHeight(),
				onBeforeSelectTabItem: function (tabid) {
					
				},
				onAfterSelectTabItem: function(tabid) {
                     loadedTabs.push(tabid);
                     switch (tabid){
	                     case "tabA":
	                    	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/toDetailPage2.do?id=${fncPrjBscInfo.id}");
	                         break;
	                     case "tabB":
                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/viewAssPackInfoDetail.do?prjid=${fncPrjBscInfo.id}&flag=2");
                             break;
                         case "tabC":
                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbuss/fncprjbscinfo/tofstpyDetailPage.do?fncPrjId=${fncPrjBscInfo.id}");
                             break;  
                         case "tabD":
                        	 pageInTab.setTabItemSrc(tabid, "<%=path%>/rfbase/fncprjpaysch/toRepayModifyPage.do?projid=${fncPrjBscInfo.id}");
                             break;
                     }
					SetIframe_height();
				}
			});
			
			$.extend(true, pageInApp, {
				pageInTab: pageInTab
			});
		};
		
		//格式化钱
		function fmoney(s, n){   
		   n = n > 0 && n <= 20 ? n : 2;   
		   s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";   
		   var l = s.split(".")[0].split("").reverse(),   
		   r = s.split(".")[1];   
		   t = "";   
		   for(var i = 0; i < l.length; i ++ )   
		   {   
		      t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");   
		   }   
		   return t.split("").reverse().join("") + "." + r;   
		} 
	    
	  	//授信详情
		function showDetailCr(id){
			Add_navTab('PrdSpl_3'+id,'授信详情','<%=path%>/rfbase/crbaseinfo/toDetailPage.do?lgcId='+id);
		}
	  	
		//报价详情
		function showDetailPrice(id){
			Add_navTab('PrdPrice'+id,'报价详情','<%=path%>/rfbase/scmbaseinfo/toDetail.do?id='+id);
		}
		
		//初始化添加合同模板
		var griddata1 = $("#cont1").ligerGrid({
			url: '<%=path%>/rfbuss/fncPrjContDetail/initList.do?contId=${fncPrjContMain.id}',
            method: 'get',
            columns: [
	            {display: '合同名称', name: 'contDetailNm', width: 180, align: 'center', enabledSort: false},
	            {display: '备注', name: 'contDetailRemark', minWidth: 200, align: 'center', enabledSort: false},
	            {display: '附件', name: 'filesList', minWidth: 240, isEllipsis: false,
                    render: function (rowdata, rowindex, value) {
                        var h='<ul class="gridNanoItems">';
                        $(value).each(function(index, el) {
                            h+='<li class="ellipsis">';
                            h+='<a class="sq-button-hasicon" href="javascript:uploadFileByUrl(';
                            h+="'"+el.filePath+"'";
                            h+=",'"+el.fileNm+"'";
                            h+=')">'+el.fileNm+'</a></li>';
                        });
                        return h+'</ul>';
                    }
                },
	            {display: '审核状态', name: 'contDetailStatusNm', width: 100, align: 'center', enabledSort: false},
	            {display: '操作', name: '', width: 150, align: 'center', enabledSort: false,
                    render:function(rowdata,rowindex,value){
						var h = "";
                    	if(rowdata.contDetailStatus != 2){//状态不等于'已审核'
 							h+='<a class="sq-button-hasicon" style="cursor:pointer;" href="javascript:modifyContract(';
                            h+="'"+rowdata.id+"'";
 							h+=",'"+rowdata.fncPrjContMainId+"'";
                            h+=')"><i class="sqBtnIco_4"></i>修改</a>';
 							h+='<a class="sq-button-hasicon" style="cursor:pointer;" href="javascript:deleteContract(';
 							h+="'"+rowdata.id+"'";
 							h+=",'"+rowdata.fncPrjContMainId+"'";
 							h+=')"><i class="sqBtnIco_7"></i>删除</a>';
                    	 }else{
                    		 h+='<font class="sq-button-hasicon" color="c9c9c9"><i class="sqBtnIco_4"></i>修改</font>';
                    		 h+='<font class="sq-button-hasicon" color="c9c9c9"><i class="sqBtnIco_7"></i>删除</font>';
                    	 }
                        return h;
                    }
                }
            ],
            usePager: false,
            root: 'rows',
            record: 'total',
   			isScroll: false, 
   	        width: '100%',
			fixedCellHeight: false,//可折行显示
   	        checkbox:true,
   	        rownumbers:true,
            onAfterShowData: function () {
            	setNanoScroller();
                SetIframe_height();
            }
		});
		
		//控制行高-加滚动条
		function setNanoScroller () {
            $('.gridNanoItems').each(function(index, el) {
                if ($(el).height() > 100 ) {
                    $(el).wrap('<div class="gridNanoContainer"><div class="nano"><div class="nano-content">');
                    $(el).parents('.gridNanoContainer').height(100);
                    $(".nano").nanoScroller({
                        preventPageScrolling: true
                    });
                };
            });
        }
		
		//下载文件方法
		function uploadFileByUrl(_url,_name){
			if(SQ.regs.img.test(_name)){
				SQ.imgPreview(_url, true);
			}else{
				window.open(_url);
			}
		}
		
		//添加合同信息
        function addContract(){
			var contId = $("#mainId").val();
        	var contDialog = $.ligerDialog.SQopen({
                width:800,
                height: 400,
                title: '合同详情',
                url: '<%=path%>/rfbuss/fncPrjContDetail/toModifyPage.do?fncPrjId=${fncPrjBscInfo.fncprjid}&contId='+contId,
                onClose: function () {
                	var json = contDialog._DATA;
                	if(null != json){
                		$("#mainId").val(json.mainId);
                	}
                	griddata1.setParm("mainId",$("#mainId").val());
                	griddata1.reload();//重新加载合同列表
                }
			});
        }
		
      	//修改合同信息
        function modifyContract(id, mainId){
        	var contDialog = $.ligerDialog.open({
                width:800,
                height: 400,
                title: '合同详情',
                url: '<%=path%>/rfbuss/fncPrjContDetail/toModifyPage.do?fncPrjId=${fncPrjBscInfo.fncprjid}&contId='+mainId+'&id='+id,
                onClose: function () {
                	griddata1.setParm("mainId",mainId);
                	griddata1.reload();//重新加载合同列表
                }
			});
        }
      	
      	//删除合同信息
   	    function deleteContract(id, mainId){
	   	    $.ligerDialog.confirm("是否确认删除选中的合同信息数据?", function (flag) {
	            if (flag == false)return;
	            var wait_dialog = null;
	            $.ajax({
	                type: 'post',
	                dataType: 'json',
	                data: {id: id, mainId: mainId},
	                url: '<%=path%>/rfbuss/fncPrjContDetail/removeById.do',
	                beforeSend: function () {
	                    wait_dialog = $.ligerDialog.SQalert({
	                        content: LogUtils.LOG_CMM_DATA_LOADING,
	                        SQ_type: "waitting"
	                    });
	                },
	                success: function (msg) {
	                    wait_dialog.close();
	                    if (msg.success == 1) {
	                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
	                        griddata1.reload();
	                    } else {
	                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
	                    }
	                }
	            });
	        });
   	    }
		
		//显示已上传但是未保存的文件列表
		function showFiles(data, index){
			var applyNm = $("#applyNm").html();
			var applyDt = $("#applyDt").html();
			var f_Grid = liger.get("ligergrid_file"+index);
			var uploadJOSN = data;
    		for (var i=0; i< uploadJOSN.length; i++){ 
        		//添加行-追加行
        		f_Grid.addRow({fileNm: uploadJOSN[i].fileNm, crtUsrRlNm: applyNm, crtTm: applyDt, doSthFlag: "1", ligerIndex: index});
    		}
		}
		
		//发起合同审核流程
		function addContAuditFlow(){
            var mainId = $("#mainId").val();
            if(null == mainId){
            	top.$.ligerDialog.SQalert({content: "合同主表ID为空,不能发起流程！",SQ_type: "warning"});
				return false;
            }
            $.ligerDialog.confirm('确定发起合同审核流程吗?', function (flag) {
				if(flag == false)return;
				var wait_dialog = null;
				var url = '<%=path%>/rfbuss/fncPrjContMain/addFlowStart.do';
				$.ajax({
					type: 'post',
					dataType: 'json',
					data: {mainId:mainId},
					url: url,
					beforeSend: function(){
						wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
					},
					success:function(msg){
						wait_dialog.close();
						if(msg.success == 1 && null != msg.data){
							top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
							Remove_navTab();//关闭当前页签
						}else{
							top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
						}
					}
				});
			});
        }
	</script>
</body>
</html>